package com.ayh.campusfun.common.core.config;

import com.ayh.campusfun.common.core.interceptor.JWTInterceptor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author ayh
 * @create 2021/3/24 0024 12:37
 * 拦截器配置
 */

@Configuration
public class InterceptorConfiguration implements WebMvcConfigurer {

    @Value("${interceptor.ignore-path}")
    private String[] ignorePaths;

    @Bean
    public JWTInterceptor jwtInterceptor() {
        return new JWTInterceptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        InterceptorRegistration interceptorRegistration = registry.addInterceptor(jwtInterceptor());
        //拦截所有请求路径
        interceptorRegistration.addPathPatterns("/**");
        for (String ignorePath : ignorePaths) {
            //排除/user/login登录请求
            interceptorRegistration.excludePathPatterns(ignorePath);
        }
    }
}
